﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Microsoft.TeamFoundation.Client;

namespace DatabaseSynchronizer {

    public static class CredentialFactory {

        public static ICredentialsProvider CreateCredentialProvider(string userName,
                                                            string domain, string password) {

            NetworkCredential networkCredential = new NetworkCredential();
            networkCredential.UserName = userName;
            networkCredential.Domain = domain;
            networkCredential.Password = password;

            return CreateCredentialProvider(networkCredential);

        }

        public static ICredentialsProvider CreateCredentialProvider(NetworkCredential networkCredential) {

            ICredentials credentials = new Credential(networkCredential);
            ICredentialsProvider provider = new CredentialProvider(credentials);

            return provider;

        }

        public static ICredentials CreateCredentials(string userName, string domain, string password) {

            NetworkCredential networkCredential = new NetworkCredential();
            networkCredential.UserName = userName;
            networkCredential.Domain = domain;
            networkCredential.Password = password;

            return new Credential(networkCredential);

        }

        public static ICredentials CreateCredentials(NetworkCredential networkCredential) {

            ICredentials credentials = new Credential(networkCredential);

            return credentials;

        }

    }


    public class CredentialProvider : ICredentialsProvider {

        public CredentialProvider(ICredentials credentials) {
            _credentials = credentials;
        }

        private ICredentials _credentials;

        public ICredentials GetCredentials(Uri uri, ICredentials failedCredentials) {
            return _credentials;
        }

        public void NotifyCredentialsAuthenticated(Uri uri) {
            //We dont care at this point
        }
    }


    public class Credential : ICredentials {

        NetworkCredential _networkCredential;

        public Credential(NetworkCredential networkCredential) {
            _networkCredential = networkCredential;
        }

        public NetworkCredential GetCredential(Uri uri, string authType) {
            return _networkCredential;
        }
    }
}
